﻿Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports DevComponents.DotNetBar
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.General
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.DAO

Partial Public Class frmMain
    Inherits Office2007RibbonForm
    Public Sub New()
        InitializeComponent()
        Dim m_frmFlash As New frmFlash()
        m_frmFlash.Show()
        System.Threading.Thread.Sleep(3000)
        m_frmFlash.Close()
        DoAnQuanLyBanHang.General.DataService.OpenConnection()
    End Sub
    Private m_NhanVienCtrl As New NhanVienCtrl()
    Private m_PhanQuyenCtrl As New PhanQuyenCtrl()
    Private m_frmDangNhap As frmDangNhap = Nothing

    Private Sub frmMain_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        ' Save Quick Access Toolbar layout if it has changed...
        If ribbonControl.QatLayoutChanged Then
            Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\DevComponents\Ribbon")
            Try
                key.SetValue("RibbonPadCSLayout", ribbonControl.QatLayout)
            Finally
                key.Close()
            End Try
        End If

    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        If MessageBoxEx.Show("Bạn có muốn thoát khỏi phần mềm quản lý siêu thị không?", "THOÁT KHỎI CHƯƠNG TRÌNH", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            Me.Close()
        End If
    End Sub
#Region "Đăng Nhập"
    Public Sub [Default]()
        btnAbout.Enabled = True
        btnCalculator.Enabled = True
        btnClose.Enabled = True
        btnHelp.Enabled = True

        btnDangNhap.Enabled = True
        btnBackUp.Enabled = False
        btnRestore.Enabled = False
        btnChangePass.Enabled = False
        btnDangXuat.Enabled = False

        btnThemMatHang.Enabled = False
        btnXuatHang.Enabled = False
        btnDatHang.Enabled = False

        btnKhachHang.Enabled = False

        btnQuayGiaoDich.Enabled = False
        btnNhanVien.Enabled = False
        btnNhomNguoiDung.Enabled = False
        btnQuyen.Enabled = False
        btnPhanNhom.Enabled = False
        btnPhanQuyen.Enabled = False
        btnDSMatHang.Enabled = False
        btnDSNhaCungCap.Enabled = False

        btnDMLoaiGiaoDich.Enabled = False
        btnDMLoaiTien.Enabled = False

        btnPhieuNhapHang.Enabled = False
        btnPhieuXuatHang.Enabled = False
        btnDonDatHang.Enabled = False
        btnDSNhanVien.Enabled = False
        btnTonKho.Enabled = False
        btnDoanhThu.Enabled = False
        btnTimNV.Enabled = False
        btnTimMatHang.Enabled = False
        btnTimNhaCC.Enabled = False
        btnTimKH.Enabled = False
    End Sub

    Public Sub Admin()
        btnHelp.Enabled = True
        btnAbout.Enabled = True
        btnCalculator.Enabled = True
        btnClose.Enabled = True
        btnDangNhap.Enabled = True
        btnBackUp.Enabled = True
        btnRestore.Enabled = True
        btnChangePass.Enabled = True
        btnDangXuat.Enabled = True

        btnThemMatHang.Enabled = True
        btnXuatHang.Enabled = True
        btnDatHang.Enabled = True

        btnKhachHang.Enabled = True

        btnQuayGiaoDich.Enabled = True
        btnNhanVien.Enabled = True

        btnNhomNguoiDung.Enabled = True
        btnQuyen.Enabled = True
        btnPhanNhom.Enabled = True
        btnPhanQuyen.Enabled = True
        btnDSMatHang.Enabled = True
        btnDSNhaCungCap.Enabled = True

        btnDMLoaiGiaoDich.Enabled = True
        btnDMLoaiTien.Enabled = True

        btnPhieuNhapHang.Enabled = True
        btnPhieuXuatHang.Enabled = True
        btnDonDatHang.Enabled = True
        btnDSNhanVien.Enabled = True
        btnTonKho.Enabled = True
        btnDoanhThu.Enabled = True
        btnTimNV.Enabled = True
        btnTimMatHang.Enabled = True
        btnTimNhaCC.Enabled = True
        btnTimKH.Enabled = True
    End Sub

    Public Sub Users()
        btnAbout.Enabled = True
        btnCalculator.Enabled = True
        btnClose.Enabled = True
        btnHelp.Enabled = True

        btnDangNhap.Enabled = True
        btnBackUp.Enabled = False
        btnRestore.Enabled = False
        btnChangePass.Enabled = True
        btnDangXuat.Enabled = True

        btnThemMatHang.Enabled = True
        btnXuatHang.Enabled = True
        btnDatHang.Enabled = True

        btnKhachHang.Enabled = True

        btnQuayGiaoDich.Enabled = True
        btnNhanVien.Enabled = True

        btnNhomNguoiDung.Enabled = False
        btnQuyen.Enabled = False
        btnPhanNhom.Enabled = False
        btnPhanQuyen.Enabled = False
        btnDSMatHang.Enabled = True
        btnDSNhaCungCap.Enabled = True

        btnDMLoaiGiaoDich.Enabled = True
        btnDMLoaiTien.Enabled = True

        btnPhieuNhapHang.Enabled = True
        btnPhieuXuatHang.Enabled = True
        btnDonDatHang.Enabled = True
        btnDSNhanVien.Enabled = True
        btnTonKho.Enabled = True
        btnDoanhThu.Enabled = True
        btnTimNV.Enabled = False
        btnTimMatHang.Enabled = True
        btnTimNhaCC.Enabled = True
        btnTimKH.Enabled = True
    End Sub
    Public Sub DangNhap()
Cont:
        If m_frmDangNhap Is Nothing OrElse m_frmDangNhap.IsDisposed Then
            m_frmDangNhap = New frmDangNhap()
        End If

        If m_frmDangNhap.ShowDialog() = DialogResult.OK Then
            If m_frmDangNhap.txtUserName.Text = "" Then
                m_frmDangNhap.lbErrorUserName.Text = "Bạn chưa nhập tên đăng nhập"
                m_frmDangNhap.lbErrorPass.Text = ""
                GoTo Cont
            End If

            If m_frmDangNhap.txtPass.Text = "" Then
                m_frmDangNhap.lbErrorPass.Text = "Bạn chưa nhập mật khẩu"
                m_frmDangNhap.lbErrorUserName.Text = ""
                GoTo Cont
            End If
            Dim kq As Integer = m_PhanQuyenCtrl.DangNhap(m_frmDangNhap.txtUserName.Text, m_frmDangNhap.txtPass.Text)
            lblTenNguoiDung.Text = "Xin chào: " & m_PhanQuyenCtrl.LayTenNguoiDung(m_frmDangNhap.txtUserName.Text)
            Select Case kq
                Case 0
                    m_frmDangNhap.lbErrorPass.Text = ""
                    m_frmDangNhap.lbErrorUserName.Text = "Người dùng không tồn tại"
                    GoTo Cont
                Case 1
                    m_frmDangNhap.lbErrorUserName.Text = ""
                    m_frmDangNhap.lbErrorPass.Text = "Mật khẩu không hợp lệ"
                    GoTo Cont
                Case 2
                    If m_PhanQuyenCtrl.PhanQuyen = "MQ001" Then
                        Admin()
                    Else
                        Users()
                    End If
                    Exit Select
            End Select
        End If

    End Sub


    Private Sub btnDangNhap_Click(sender As System.Object, e As System.EventArgs) Handles btnDangNhap.Click
        m_frmDangNhap.txtUserName.Text = ""
        m_frmDangNhap.txtPass.Text = ""
        m_frmDangNhap.lbErrorPass.Text = ""
        m_frmDangNhap.lbErrorUserName.Text = ""

        DangNhap()
    End Sub

    Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        [Default]()

        DangNhap()

        ' Load Quick Access Toolbar layout if one is saved from last session...
        Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\DevComponents\Ribbon")
        If key IsNot Nothing Then
            Try
                Dim layout As String = key.GetValue("RibbonPadCSLayout", "").ToString()
                If layout <> "" AndAlso layout IsNot Nothing Then
                    ribbonControl.QatLayout = layout
                End If
            Finally
                key.Close()
            End Try
        End If

        ' Pulse the Application Button
        buttonFile.Pulse(11)

    End Sub

    Private Sub btnDangXuat_Click(sender As System.Object, e As System.EventArgs) Handles btnDangXuat.Click
        [Default]()
        lblTenNguoiDung.Text = "Bạn cần đăng nhập để sử dụng phần mềm"
    End Sub
#End Region

#Region "Đổi mật khẩu"
    Private m_frmDoiMatKhau As frmDoiMatKhau = Nothing
    Public Sub DoiMatKhau()
Cont:
        If m_frmDoiMatKhau.ShowDialog() = DialogResult.OK Then
            If m_frmDoiMatKhau.txtMatKhauCu.Text = "" Then
                m_frmDoiMatKhau.lblMatKhauCu.Text = "Chưa nhập mật khẩu cũ"
                m_frmDoiMatKhau.lblMatKhauMoi.Text = ""
                m_frmDoiMatKhau.lblNhapLaiMatKhauMoi.Text = ""

                GoTo Cont
            End If

            If m_frmDoiMatKhau.txtMatKhauMoi.Text = "" Then
                m_frmDoiMatKhau.lblMatKhauMoi.Text = "Chưa nhập mật khẩu mới"
                m_frmDoiMatKhau.lblMatKhauCu.Text = ""
                m_frmDoiMatKhau.lblNhapLaiMatKhauMoi.Text = ""

                GoTo Cont
            End If

            If m_frmDoiMatKhau.txtNhapLaiMatKhauMoi.Text = "" Then
                m_frmDoiMatKhau.lblNhapLaiMatKhauMoi.Text = "Chưa nhập lại mật khẩu mới"
                m_frmDoiMatKhau.lblMatKhauMoi.Text = ""
                m_frmDoiMatKhau.lblMatKhauCu.Text = ""

                GoTo Cont
            End If

            Dim m_TenDangNhap As [String] = m_frmDangNhap.txtUserName.Text
            Dim m_MatKhau As [String] = m_frmDangNhap.txtPass.Text

            Dim m_MatKhauCu As [String] = m_frmDoiMatKhau.txtMatKhauCu.Text
            Dim m_MatKhauMoi As [String] = m_frmDoiMatKhau.txtMatKhauMoi.Text
            Dim m_NhapLaiMatKhauMoi As [String] = m_frmDoiMatKhau.txtNhapLaiMatKhauMoi.Text

            If m_MatKhauCu <> m_MatKhau Then
                m_frmDoiMatKhau.lblMatKhauCu.Text = "Nhập mật khẩu cũ không khớp"
                m_frmDoiMatKhau.lblMatKhauMoi.Text = ""
                m_frmDoiMatKhau.lblNhapLaiMatKhauMoi.Text = ""
                GoTo Cont

            ElseIf m_MatKhauMoi <> m_NhapLaiMatKhauMoi Then
                m_frmDoiMatKhau.lblNhapLaiMatKhauMoi.Text = "Nhập lại mật khẩu không khớp"
                m_frmDoiMatKhau.lblMatKhauMoi.Text = ""
                m_frmDoiMatKhau.lblMatKhauCu.Text = ""
                GoTo Cont
            Else
                m_PhanQuyenCtrl.DoiMatKhau(m_TenDangNhap, m_MatKhauMoi)
                MessageBoxEx.Show("Đổi mật khẩu thành công!", "Đổi Mật Khẩu", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End If
    End Sub

    Private Sub btnChangePass_Click(sender As System.Object, e As System.EventArgs) Handles btnChangePass.Click
        If m_frmDoiMatKhau Is Nothing OrElse m_frmDoiMatKhau.IsDisposed Then
            m_frmDoiMatKhau = New frmDoiMatKhau()

            m_frmDoiMatKhau.txtMatKhauCu.Text = ""
            m_frmDoiMatKhau.txtMatKhauMoi.Text = ""
            m_frmDoiMatKhau.txtNhapLaiMatKhauMoi.Text = ""

            m_frmDoiMatKhau.lblMatKhauCu.Text = ""
            m_frmDoiMatKhau.lblMatKhauMoi.Text = ""
            m_frmDoiMatKhau.lblNhapLaiMatKhauMoi.Text = ""

            DoiMatKhau()
        End If
    End Sub
#End Region

#Region "sao lưu dữ liệu:"
    Private Sub btnBackUp_Click(sender As System.Object, e As System.EventArgs) Handles btnBackUp.Click
        Try
            Dim save As SaveFileDialog = New SaveFileDialog()
            save.Filter = "Database (*.bak)|*.bak"
            save.ShowDialog()
            Dim path As String = save.FileName
            Dim sqlBackup As String = "BACKUP DATABASE [QUANLYBANHANG-1065007-1065043] TO DISK='" + path + "'"
            Dim ckn As String = DataService.m_ConnectString
            Dim con As SqlConnection = New SqlConnection(ckn)
            con.Open()
            Dim cmd As SqlCommand = New SqlCommand(sqlBackup, con)
            cmd.ExecuteNonQuery()
            con.Close()
            MessageBox.Show("Đã backup cơ sở dữ liệu đến " + path + "")
        Catch ex As SqlException
            MessageBox.Show(ex.Message, "Backup Database loi ")
            Return
        End Try

    End Sub


    Private Sub btnRestore_Click(sender As System.Object, e As System.EventArgs) Handles btnRestore.Click
        Try
            Dim open As New OpenFileDialog
            open.Filter = "Database (*.bak)|*.bak"
            open.ShowDialog()
            Dim path As String = open.FileName
            Dim sqlRestore As String = "ALTER DATABASE [QUANLYBANHANG-1065007-1065043] SET SINGLE_USER WITH ROLLBACK IMMEDIATE" + Environment.NewLine + _
                "Use master Restore Database [QUANLYBANHANG-1065007-1065043] from disk='" + path + "' with replace"
            Dim ckn As String = DataService.m_ConnectString
            Dim con As SqlConnection = New SqlConnection(ckn)
            con.Open()
            Dim cmd As SqlCommand = New SqlCommand(sqlRestore, con)
            cmd.ExecuteNonQuery()
            con.Close()
            MessageBox.Show("Database đã được khôi phục", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As SqlException
            MessageBox.Show(ex.Message, "Restore Database Eror")
            Return
        End Try

    End Sub
#End Region

#Region "Menu Quản Lý:"

    Private Sub btnThemMatHang_Click(sender As System.Object, e As System.EventArgs) Handles btnThemMatHang.Click
        LoadMain.HienThiMatHang()
    End Sub

    Private Sub btnDatHang_Click(sender As System.Object, e As System.EventArgs) Handles btnDatHang.Click
        LoadMain.HienThiDatHang()
    End Sub

    Private Sub btnXuatHang_Click(sender As System.Object, e As System.EventArgs) Handles btnXuatHang.Click
        LoadMain.HienThiXuatHang()
    End Sub

    Private Sub btnKhachHang_Click(sender As System.Object, e As System.EventArgs) Handles btnKhachHang.Click
        LoadMain.HienThiKhachHang()
    End Sub

    Private Sub btnQuayGiaoDich_Click(sender As System.Object, e As System.EventArgs) Handles btnQuayGiaoDich.Click
        LoadMain.HienThiQuayGiaoDich()
    End Sub

    Private Sub btnNhanVien_Click(sender As System.Object, e As System.EventArgs) Handles btnNhanVien.Click
        LoadMain.HienThiNhanVien()
    End Sub
#End Region

#Region "Menu Chi tiết:"
    Private Sub btnDSMatHang_Click(sender As System.Object, e As System.EventArgs) Handles btnDSMatHang.Click
        LoadMain.HienThiDSMatHang()

    End Sub

    Private Sub btnDSNhaCungCap_Click(sender As System.Object, e As System.EventArgs) Handles btnDSNhaCungCap.Click
        LoadMain.HienThiNhaCungCap()
    End Sub

    Private Sub btnDMLoaiGiaoDich_Click(sender As System.Object, e As System.EventArgs) Handles btnDMLoaiGiaoDich.Click
        LoadMain.HienThiLoaiGiaoDich()
    End Sub

    Private Sub btnDMLoaiTien_Click(sender As System.Object, e As System.EventArgs) Handles btnDMLoaiTien.Click
        LoadMain.HienThiLoaiTien()
    End Sub
#End Region

#Region "Menu Tìm kiếm:"

    Private Sub btnTimNV_Click(sender As System.Object, e As System.EventArgs) Handles btnTimNV.Click
        LoadMain.HienThiTimKiemNhanVien()
    End Sub

    Private Sub btnTimMatHang_Click(sender As System.Object, e As System.EventArgs) Handles btnTimMatHang.Click
        LoadMain.HienThiTimKiemMatHang()
    End Sub

    Private Sub btnTimNhaCC_Click(sender As System.Object, e As System.EventArgs) Handles btnTimNhaCC.Click
        LoadMain.HienThiTimNhaCungCap()
    End Sub

    Private Sub btnTimKH_Click(sender As System.Object, e As System.EventArgs) Handles btnTimKH.Click
        LoadMain.HienThiTimKiemKhachHang()
    End Sub
#End Region

#Region "Menu Thống Kê:"
    Private Sub btnDSNhanVien_Click(sender As System.Object, e As System.EventArgs) Handles btnDSNhanVien.Click
        LoadMain.HienThiDSNhanVien()
    End Sub

    Private Sub btnDoanhThu_Click(sender As System.Object, e As System.EventArgs) Handles btnDoanhThu.Click
        LoadMain.HienThiDoanhThu()
    End Sub

    Private Sub btnTonKho_Click(sender As System.Object, e As System.EventArgs) Handles btnTonKho.Click
        LoadMain.HienThiTonKho()
    End Sub
#End Region

#Region "Menu Lập Phiếu"
    Private Sub btnPhieuNhapHang_Click(sender As System.Object, e As System.EventArgs) Handles btnPhieuNhapHang.Click
        LoadMain.HienThiPhieuNhapHang()

    End Sub

    Private Sub btnDonDatHang_Click(sender As System.Object, e As System.EventArgs) Handles btnDonDatHang.Click
        LoadMain.HienThiDonDatHang()
    End Sub

    Private Sub btnPhieuXuatHang_Click(sender As System.Object, e As System.EventArgs) Handles btnPhieuXuatHang.Click
        LoadMain.HienThiPhieuXuatHang()
    End Sub
#End Region

#Region "Menu Quản Trị"

    Private Sub btnNhomNguoiDung_Click(sender As System.Object, e As System.EventArgs) Handles btnNhomNguoiDung.Click
        LoadMain.HienThiNhomNguoiDung()
    End Sub

    Private Sub btnQuyen_Click(sender As System.Object, e As System.EventArgs) Handles btnQuyen.Click
        LoadMain.HienThiQuyen()
    End Sub

    Private Sub btnPhanNhom_Click(sender As System.Object, e As System.EventArgs) Handles btnPhanNhom.Click
        LoadMain.HienThiPhanNhomNguoiDung()
    End Sub

    Private Sub btnPhanQuyen_Click(sender As System.Object, e As System.EventArgs) Handles btnPhanQuyen.Click
        LoadMain.HienThiPhanQuyen()
    End Sub
#End Region

#Region "Menu Trợ Giúp"

    Private Sub btnHelp_Click(sender As System.Object, e As System.EventArgs) Handles btnHelp.Click
        Help.ShowHelp(Me, "help.chm")

    End Sub

    Private Sub btnCalculator_Click(sender As System.Object, e As System.EventArgs) Handles btnCalculator.Click
        Process.Start("calc.exe")
    End Sub

    Private Sub btnAbout_Click(sender As System.Object, e As System.EventArgs) Handles btnAbout.Click
        Dim m_frmAbout As New frmAbout()
        m_frmAbout.Show()
    End Sub
#End Region

    Private Sub ribbonTabItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ribbonTabItem6.Click

    End Sub
End Class
